//
//  YCMeHomeViewController.m
//  YCSchool
//
//  Created by gmall on 16/5/9.
//  Copyright © 2016年 yanglinxia. All rights reserved.
//

#import "YCMeHomeViewController.h"
#import "YCMeHomeMsgContentView.h"
#import <FXBlurView/FXBlurView.h>
#import "YCMeMessageViewController.h"
#import "YCMyIdentityViewController.h"
#import "YCArticlelistViewController.h"

#import "YCNavigationViewController.h"
#import "YCHomeViewController.h"
#import "YCSessionViewController.h"

@interface YCMeHomeViewController ()

/** scrollView  */
@property (nonatomic, strong) UIScrollView *scrollView;
/** 头像大背景  */
@property (nonatomic, strong) UIImageView *contentImageView;


/// 我
@property (nonatomic,readonly) UIButton *meButton;
/// 我的订单
@property (nonatomic,readonly) UIButton *myOrderButton;
/// 头像小背景
@property (nonatomic,readonly) UIImageView *iconBgImageView;
/// 头像
@property (nonatomic,readonly) UIImageView *iconImageView;

/// 昵称+星座
@property (nonatomic,readonly) UILabel *nickerAstroLabel;
/// 地址
@property (nonatomic,readonly) UILabel *placeLabel;

/// 新消息
@property (nonatomic,readonly) UIButton *msgButton;
/// 无新消息
@property (nonatomic,readonly) UIButton *msgIconButton;
/// 底部消息内容
@property (nonatomic,readonly) YCMeHomeMsgContentView *msgContentView;

/** 底部条  */
@property (nonatomic, strong) UIView *bottomView;

/** msgBtn badgeview  */
@property (nonatomic, strong) JSBadgeView *msgButtonBadgeView;

@end

@implementation YCMeHomeViewController

@synthesize meButton = _meButton;
@synthesize myOrderButton = _myOrderButton;
@synthesize nickerAstroLabel = _nickerAstroLabel;
@synthesize placeLabel = _placeLabel;
@synthesize iconBgImageView = _iconBgImageView;
@synthesize iconImageView = _iconImageView;
@synthesize msgButton = _msgButton;
@synthesize msgIconButton = _msgIconButton;
@synthesize msgContentView = _msgContentView;
// 底部条
- (UIView *)bottomView
{
    if (!_bottomView) {
        _bottomView = [[UIView alloc] init];
        
        UIButton *button1 = [[UIButton alloc] init];
        [button1 setImage:[UIImage imageNamed:@"一乘未选中"] forState:UIControlStateNormal];
        [button1 setImage:[UIImage imageNamed:@"一乘"] forState:UIControlStateSelected];
        button1.imageView.contentMode = UIViewContentModeCenter;
        
        
        UIButton *button2 = [[UIButton alloc] init];
        [button2 setImage:[UIImage imageNamed:@"聊天"] forState:UIControlStateNormal];
        [button2 setImage:[UIImage imageNamed:@"聊天选中"] forState:UIControlStateSelected];
        button2.imageView.contentMode = UIViewContentModeCenter;
        
        UIButton *button3 = [[UIButton alloc] init];
        [button3 setImage:[UIImage imageNamed:@"我"] forState:UIControlStateNormal];
        button3.selected = YES;
        button3.highlighted = YES;
        button3.imageView.contentMode = UIViewContentModeCenter;
        
        // 分隔线
        UIView *line = [[UIView alloc] init];
        line.backgroundColor = [UIColor colorWithWhite:0.5 alpha:0.3];
        
        [_bottomView addSubview:button1];
        [_bottomView addSubview:button2];
        [_bottomView addSubview:button3];
        [_bottomView addSubview:line];
        
        [button1 addTarget:self action:@selector(didTouchUpInsideLeftButton:) forControlEvents:UIControlEventTouchUpInside];
        [button2 addTarget:self action:@selector(didTouchUpInsideMiddleButton:) forControlEvents:UIControlEventTouchUpInside];
        [button3 addTarget:self action:@selector(didTouchUpInsideRightButton:) forControlEvents:UIControlEventTouchUpInside];
        
        [button1 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_bottomView.mas_left).offset(20);
            make.centerY.equalTo(_bottomView);
            make.width.and.height.equalTo(@50);
        }];
        
        [button2 mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.centerX.and.centerY.equalTo(_bottomView);
            make.width.and.height.equalTo(@50);
        }];
        
        [button3 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(_bottomView.mas_right).offset(-20);
            make.centerY.equalTo(_bottomView);
            make.width.and.height.equalTo(@50);
        }];
        
        [line mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.left.right.equalTo(_bottomView);
            make.height.equalTo(@1);
        }];
    }
    return _bottomView;
}

/// 我
- (UIButton *)meButton
{
    if(!_meButton)
    {
        _meButton = [[UIButton alloc] init];
        _meButton.titleLabel.font = [UIFont boldSystemFontOfSize:15];
        [_meButton setTitleColor:[UIColor yc_colorWithUInt:0xffffff alpha:0.7] forState:UIControlStateNormal];
        _meButton.layer.cornerRadius = 17.5;
        [_meButton setTitle:NSLocalizedString(@"我", nil) forState:UIControlStateNormal];
        [_meButton addTarget:self action:@selector(didTouchUpInsideMeButton:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _meButton;
}

/// 我的订单
- (UIButton *)myOrderButton
{
    if(!_myOrderButton)
    {
        _myOrderButton = [[UIButton alloc] init];
        _myOrderButton.titleLabel.font = [UIFont boldSystemFontOfSize:14];
        [_meButton setTitleColor:[UIColor yc_colorWithUInt:0xffffff alpha:0.7] forState:UIControlStateNormal];
        [_myOrderButton setTitle:NSLocalizedString(@"我的订单", nil) forState:UIControlStateNormal];
        [_myOrderButton setImage:[UIImage imageNamed:@"我的订单"] forState:UIControlStateNormal];
        [_myOrderButton addTarget:self action:@selector(didTouchUpInsideMyOrderButton:) forControlEvents:UIControlEventTouchUpInside];
        _myOrderButton.imageEdgeInsets = UIEdgeInsetsMake(0, -7, 0, 0);
    }
    return _myOrderButton;
}
// 昵称与星座
- (UILabel *)nickerAstroLabel
{
    if(!_nickerAstroLabel)
    {
        _nickerAstroLabel = [[UILabel alloc] init];
        _nickerAstroLabel.text = @"Leelan 狮子座";
        _nickerAstroLabel.textColor = [UIColor yc_colorWithUInt:0xffffff alpha:0.8];
        _nickerAstroLabel.textAlignment = NSTextAlignmentCenter;
        _nickerAstroLabel.font = [UIFont systemFontOfSize:15];
//        _nickerAstroLabel.hidden = YES;
    }
    return _nickerAstroLabel;
}

// 地址
- (UILabel *)placeLabel
{
    if(!_placeLabel)
    {
        _placeLabel = [[UILabel alloc] init];
        _placeLabel.text = @"广东 广州";
        _placeLabel.textColor = [UIColor yc_colorWithUInt:0xffffff alpha:0.8];
        _placeLabel.textAlignment = NSTextAlignmentCenter;
        _placeLabel.font = [UIFont systemFontOfSize:13];
//        _placeLabel.hidden = YES;
    }
    return _placeLabel;
}
// 头像
- (UIImageView *)iconImageView
{
    if(!_iconImageView)
    {
        _iconImageView = [[UIImageView alloc] init];
//        _iconImageView.image = [UIImage imageNamed:@"选择相册"];
        _iconImageView.image = [UIImage imageNamed:@"mf_bg_img"];
        __block CGFloat width = 0;
        [YCAdapter performOtherBlock:^{
            width = 165;
        } IPhone6Block:^{
            width = 195;
        } IPhone6pBlock:^{
            width = 205;
        }];
        _iconImageView.layer.cornerRadius = width / 2.0;
        _iconImageView.layer.masksToBounds = YES;
        _iconImageView.userInteractionEnabled = YES;
        _iconImageView.contentMode = UIViewContentModeScaleAspectFill;
        
        UITapGestureRecognizer *tapIcon = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapIcon:)];
        [_iconImageView addGestureRecognizer:tapIcon];
    }
    return _iconImageView;
}

// 头像背景
- (UIImageView *)iconBgImageView
{
    if(!_iconBgImageView)
    {
        _iconBgImageView = [[UIImageView alloc] init];
        _iconBgImageView.image = [UIImage imageNamed:@"头像框"];
        _iconBgImageView.userInteractionEnabled = YES;
        _iconBgImageView.layer.masksToBounds = YES;
        __block CGFloat width = 0;
        [YCAdapter performOtherBlock:^{
            width = 190;
        } IPhone6Block:^{
            width = 232;
        } IPhone6pBlock:^{
            width = 240;
        }];
        _iconBgImageView.layer.cornerRadius = width / 2.0;
        _iconBgImageView.contentMode = UIViewContentModeScaleAspectFill;
        _iconBgImageView.clipsToBounds = YES;
    }
    return _iconBgImageView;
}

/// 新消息
- (UIButton *)msgButton
{
    if(!_msgButton)
    {
        _msgButton = [[UIButton alloc] init];
        _msgButton.titleLabel.font = [UIFont boldSystemFontOfSize:14];
        [_msgButton setTitleColor:[UIColor yc_colorWithUInt:0x333333 alpha:0.8] forState:UIControlStateNormal];
        _msgButton.backgroundColor = [UIColor yc_colorWithUInt:0xffffff alpha:0.7];
        [_msgButton setTitle:NSLocalizedString(@"新消息", nil) forState:UIControlStateNormal];
        _msgButton.layer.cornerRadius = 13;
        _msgButton.layer.masksToBounds = YES;
        _msgButton.clipsToBounds = NO;
        [_msgButton addTarget:self action:@selector(didTouchUpInsideNewMsgButton:) forControlEvents:UIControlEventTouchUpInside];
        
        // 添加badgeview
        JSBadgeView *badgeView = [[JSBadgeView alloc] initWithParentView:_msgButton alignment:JSBadgeViewAlignmentTopRight];
        badgeView.badgeBackgroundColor = [UIColor redColor];
        badgeView.badgeTextFont = [UIFont systemFontOfSize:18];
        
        // 计算偏移距离
        badgeView.badgePositionAdjustment = CGPointMake(-15,5);
        badgeView.badgeText = @"1";// 等于nil,不会显示
        self.msgButtonBadgeView = badgeView;
    }
    return _msgButton;
}

/// 无新消息
- (UIButton *)msgIconButton
{
    if(!_msgIconButton)
    {
        _msgIconButton = [[UIButton alloc] init];
        [_msgIconButton setImage:[UIImage imageNamed:@"新消息"] forState:UIControlStateNormal];
//        [_msgIconButton addTarget:self action:@selector(didTouchUpInsideMyOrderButton:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _msgIconButton;
}

- (YCMeHomeMsgContentView *)msgContentView
{
    if(!_msgContentView)
    {
        _msgContentView = [[YCMeHomeMsgContentView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 99) contentViewClickBlock:^{
            //跳到文章 颜值
            YCArticlelistViewController *articlelistVc = [[YCArticlelistViewController alloc] init];
            [self.navigationController pushViewController:articlelistVc animated:YES];
            
        }];
        _msgContentView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.6];
    }
    return _msgContentView;
}

#pragma mark - 生命周期

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self yc_addBackBarButtonItem];
    self.view.backgroundColor = [UIColor yc_colorWithUInt:0xf7f7f7];
    [self makeView];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:YES];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

- (void)makeView
{
    [self.view addSubview:self.bottomView];
    [self.bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self.view);
        make.height.equalTo(@50);
    }];
    
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 50)];
    self.scrollView.alwaysBounceVertical = NO;
    [self.view addSubview:self.scrollView];
    self.scrollView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
    [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self.view);
        make.bottom.equalTo(self.bottomView.mas_top);
    }];
    
    UIImageView *contentImageView = [[UIImageView alloc] init];
    contentImageView.backgroundColor = [UIColor yc_colorWithUInt:0xf7f7f7];
    contentImageView.image = [UIImage imageNamed:@"mf_bg_img"];
    contentImageView.userInteractionEnabled = YES;
    //模糊效果
    FXBlurView * aview = [[FXBlurView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    aview.tintColor = [UIColor clearColor];  //前景颜色
    aview.blurRadius = 10;                 //模糊半径，View的模糊效果程度
    [contentImageView addSubview:aview];
    self.contentImageView = contentImageView;
    [self.scrollView addSubview:contentImageView];
    [self.contentImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self.scrollView);
        make.width.equalTo(self.scrollView);
    }];
    
    [self.contentImageView addSubview:self.meButton];
    [self.contentImageView addSubview:self.myOrderButton];
    [self.contentImageView addSubview:self.iconBgImageView];
    [self.contentImageView addSubview:self.iconImageView];
    [self.contentImageView addSubview:self.nickerAstroLabel];
    [self.contentImageView addSubview:self.placeLabel];
    [self.contentImageView addSubview:self.msgButton];
    [self.contentImageView addSubview:self.msgIconButton];
    [self.contentImageView addSubview:self.msgContentView];
    
    [_meButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_contentImageView).offset(heightFromIphone6Height(38));
        make.left.equalTo(_contentImageView).offset(15);//标注是21
        make.height.equalTo(@15);
    }];
    [_myOrderButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_meButton);
        make.right.equalTo(_contentImageView).offset(-15);
        make.height.equalTo(@16);
    }];
    
    [_iconBgImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_contentImageView).offset(heightFromIphone6Height(93));
        make.centerX.equalTo(self.contentImageView);
        [YCAdapter performOtherBlock:^{
            make.width.height.equalTo(@190);
        } IPhone6Block:^{
            make.width.height.equalTo(@232);
        } IPhone6pBlock:^{
            make.width.height.equalTo(@240);
        }];
    }];
    
    [_iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        [YCAdapter performOtherBlock:^{
            make.width.height.equalTo(@165);
        } IPhone6Block:^{
            make.width.height.equalTo(@195);
        } IPhone6pBlock:^{
            make.width.height.equalTo(@205);
        }];
        make.center.equalTo(_iconBgImageView);
    }];
    
    [_nickerAstroLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_iconBgImageView.mas_bottom).offset(heightFromIphone6Height(22));
        make.centerX.equalTo(_iconBgImageView);
        make.height.equalTo(@15);
    }];
    
    [_placeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_nickerAstroLabel.mas_bottom).offset(heightFromIphone6Height(11));
        make.centerX.equalTo(_iconBgImageView);
        make.height.equalTo(@13);
    }];
    
    // 无新消息--图片44*65
    [_msgIconButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_placeLabel.mas_bottom).offset(heightFromIphone6Height(22));
        make.centerX.equalTo(self.contentImageView);
        make.width.equalTo(@22);
        make.height.equalTo(@33);
    }];
    
    // 新消息-文字
    [_msgButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_msgIconButton);
        make.centerX.equalTo(self.contentImageView);
        make.width.equalTo(@129);
        make.height.equalTo(@30);
    }];
    
    [_msgContentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_msgButton.mas_bottom).with.offset(heightFromIphone6Height(29));
        make.left.equalTo(self.contentImageView);
        make.right.equalTo(self.contentImageView);
        make.height.equalTo(@99);
    }];
    
    [self.contentImageView layoutIfNeeded];
    CGFloat contentH = CGRectGetMaxY(_msgContentView.frame);
    [self.contentImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        //        make.bottom.equalTo(_logoImageView.mas_bottom).offset(heightFromIphone6Height(63));
        make.bottom.equalTo(_msgContentView.mas_bottom).offset(kScreenHeight - contentH);
    }];
    
    self.scrollView.contentSize = CGSizeMake(0, CGRectGetMaxY(self.contentImageView.frame)-64);
}


#pragma mark -- 响应事件

/// 我
- (void)didTouchUpInsideMeButton:(UIButton *)button
{
    YCLog(@"我");
    button.selected = YES;
    
}
/// 我的订单
- (void)didTouchUpInsideMyOrderButton:(UIButton *)button
{
    YCLog(@"我的订单");
}

// 新消息
- (void)didTouchUpInsideNewMsgButton:(UIButton *)btn
{
    YCLog(@"新消息");
    YCMeMessageViewController *msgVc = [[YCMeMessageViewController alloc] init];
    [self presentViewController:msgVc animated:YES completion:nil];
}

// 点击头像-设置头像
- (void)tapIcon:(UITapGestureRecognizer *)tap
{
    YCLog(@"点击头像");
    YCMyIdentityViewController *myIdentityVc = [[YCMyIdentityViewController alloc] init];
    [self.navigationController pushViewController:myIdentityVc animated:YES];
    
    
}

// 底部条按钮点击事件
- (void)didTouchUpInsideLeftButton:(UIButton *)button
{
    YCLogFunc
    YCHomeViewController *rootVc = [[YCHomeViewController alloc] init];
    YCNavigationViewController *nav = [[YCNavigationViewController alloc] initWithRootViewController:rootVc];
    [UIApplication sharedApplication].keyWindow.rootViewController = nav;
}

// 聊天
- (void)didTouchUpInsideMiddleButton:(UIButton *)button
{
    YCLogFunc
    YCSessionViewController *rootVc = [[YCSessionViewController alloc] init];
    YCNavigationViewController *nav = [[YCNavigationViewController alloc] initWithRootViewController:rootVc];
    [UIApplication sharedApplication].keyWindow.rootViewController = nav;
}

//个人页
- (void)didTouchUpInsideRightButton:(UIButton *)button
{
//    UIViewController *rootVc = [[YCMeHomeViewController alloc] init];
//    YCNavigationViewController *nav = [[YCNavigationViewController alloc] initWithRootViewController:rootVc];
//    [UIApplication sharedApplication].keyWindow.rootViewController = nav;
}


@end
